<?php

namespace helpers;

use core\App;

App::uses('mpdf','vendor/mpdf');
//error_reporting(0);
class PdfHelper extends \mPDF
{
    private $files = [];

    private $write_body = null;

    public $allow_charset_conversion = true;

    public $charset_in = 'iso-88859-1';

    private $_title = null;

    private $style_page = null;

    private $_header = '';
    private $_html_header = '';
    private $_enabled_header = 1;

    private $_footer = '';
    private $_html_footer = '';
    private $_enabled_footer = 1;


    public $name = 'arquivo.pdf';

    public function __construct($default_font_size=10,$default_font=10,$mgl=10,$mgr=10,$mgt=20,$mgb=20,$mgh=9,$mgf=9){
        $this->init($default_font_size,$default_font,$mgl,$mgr,$mgt,$mgb,$mgh,$mgf);
    }

    public function init($default_font_size,$default_font,$margin_l,$margin_r,$margin_t,$margin_b,$margin_h,$margin_f){
        parent::mPDF('pt','A4',$default_font_size,$default_font,$margin_l,$margin_r,$margin_t,$margin_b,$margin_h,$margin_f,'P');
    }

    public function getFileCss($files=[])
    {
        $dirCss = ASSETS."css/";
        if(is_array($files))
        {
            foreach ($files as $file)
            {
                $this->files[] = file_get_contents($dirCss.$file);
            }
        }
        else
        {
            $this->files = file_get_contents($dirCss.$files);
        }
    }

    public function title($name=null){
        if(!empty($name)){
            $this->_title = $name;
        }
    }

    private function getTitle(){
        if(empty($this->_title)){
            $this->_title = "Relat�rio";
        }
        return $this->_title;
    }

    public function headerPage($title = null){
        if(!empty($title))
        {
            $this->_header = utf8_encode($title);
        }
    }

    public function enabledHeader($type=1){
        $this->_enabled_header = $type;
    }

    private function getEnabledHeader(){
        return $this->_enabled_header;
    }

    public function enabledFooter($type=1){
        $this->_enabled_footer = $type;
    }

    private function getEnabledFooter(){
        return $this->_enabled_footer;
    }

    private function getHeaderPage(){
        if (empty($this->_header)) {
            $this->_header = utf8_encode("{DATE j/m/Y H:i}|{PAGENO}/{nb}| {$this->getTitle()}");
        }
        return $this->_header;
    }

    public function footerPage($title = null){
        if(!empty($title)) {
            $this->_footer = utf8_encode($title);
        }
    }

    public function getFooterPage(){
        if (empty($this->_footer)) {
            $this->_footer = utf8_encode('{DATE j/m/Y H:i}|{PAGENO}/{nb}| UNIMED TERESINA / (86)2107-8000');
        }
        return $this->_footer;
    }

    public function writeCss()
    {
        if(!empty($this->files)) {
            if (is_array($this->files)) {
                foreach ($this->files as $file) {
                    $this->WriteHTML($file, 1);
                }
            } else {
                $this->WriteHTML($this->files, 1);
            }
        }
    }

    public function writeBody($html)
    {
        ob_start();
        echo $html;
        $body = ob_get_clean();
        $this->write_body = $body;
    }

    public function setStylePage($type = 0)
    {
        switch($type)
        {
            case 0:
                $this->style_page = 'P';
                break;
            case 1:
                $this->style_page = 'L';
                break;
            default:
                $this->style_page = 'P';
                break;
        }
        return $this->style_page;
    }

    private function getStylePage(){
        if($this->style_page === 'L'){
            $format = 'A4-L';
            $orientation = 'L';
        }else{
            $format = 'A4';
            $orientation = 'P';
        }
        return $this->_setPageSize($format,$orientation);
    }

    public function nameRand($name=null,$rand=true){
        if(!empty($name) && $rand === true) {
            $this->name = date('YmdHis') . $name;
        }else{
            $this->name = $name;
        }
        return $this->name;
    }

    public function htmlHeader($header){
        $this->_html_header = $header;
    }

    private function getHtmlHeader(){
        return $this->_html_header;
    }

    public function htmlFooter($footer,$n_page=false){
        $html_npage = '';
        if($n_page == true){
            $html_npage = '<div class="n-page-rodape">
            <div class="text-pagenate-left">'.$this->getTitle().'&nbsp;</div>
            <div class="text-pagenate">{PAGENO} / {nb}</div>
            <div class="text-pagenate-right">'.date("d/m/Y H:i:s").'</div>
            </div>';
        }
        $this->_html_footer = utf8_encode($html_npage.$footer);
    }

    private function getHtmlFooter(){
        return $this->_html_footer;
    }

    /**
     * @param string $type
     * I: send the file inline to the browser. The plug-in is used if available. The name given by filename is used when one selects the "Save as" option on the link generating the PDF.
     * D: send to the browser and force a file download with the name given by filename.
     * F: save to a local file with the name given by filename (may include a path).
     * S: return the document as a string. filename is ignored.
     */
    public function save($type = 'D', $path="")
    {
        $htmlHeader = $this->getHtmlHeader();
        if(!empty($htmlHeader)){
            $this->SetHTMLHeader($htmlHeader);
        }else {
            ($this->getEnabledHeader()) ? $this->SetHeader($this->getHeaderPage()) : false;
        }

        $htmlFooter = $this->getHtmlFooter();
        if(!empty($htmlFooter)){
            $this->SetHTMLFooter($htmlFooter,'O');
        }else {
            ($this->getEnabledFooter())? $this->SetFooter($this->getFooterPage()):false;
        }

        $this->getStylePage();
        $this->SetTitle($this->_title);
        $this->writeCss();
        $this->WriteHTML(utf8_encode($this->write_body),2);
        $this->SetDisplayMode('fullpage');

        $this->Output($path.$this->name,$type);
    }

} 